﻿Imports System.Data.OleDb
Public Class frmAppointments
    Private Sub btnSubmit_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click
        Dim connection As OleDb.OleDbConnection

        Dim mydb, mystr As String

        mystr = ("Provider=Microsoft.JET.OLEDB.4.0;" & _
                 "Data Source=|DataDirectory|\Decisions.mdb")

        connection = New OleDb.OleDbConnection(mystr)

        connection.Open()

        mydb = "INSERT INTO tblDecisions (fldDate,fldTime,fldRealTime,fldOnTime,fldSmallDelay,fldLargeDelay,fldEncouragement,fldCourage,fldSupport,fldAcknowledgement,fldSecurity,fldUnderstanding,fldCaring,fldTrust,fldConfidence,fldAcceptance,fldInvolvement,fldSharing,fldAngry,fldComments) values ('" & AppointmentDatePicker.Text & "','" & AppointmentTimePicker.Text & "','" & AppointmentRealTimePicker.Text & "','" & chkOnTime.CheckState & "','" & chkSmallDelay.CheckState & "','" & chkLargeDelay.CheckState & "','" & chkEncouragement.CheckState & "','" & chkCourage.CheckState & "','" & chkSupport.CheckState & "','" & chkAcknowledgement.CheckState & "','" & chkSecurity.CheckState & "','" & chkUnderstanding.CheckState & "','" & chkCaring.CheckState & "','" & chkTrust.CheckState & "','" & chkConfidence.CheckState & "','" & chkAcceptance.CheckState & "','" & chkInvolvement.CheckState & "','" & chkSharing.CheckState & "','" & chkAngry.CheckState & "','" & txtComments.Text & "')"

        Dim run = New OleDb.OleDbCommand

        Try

            run = New OleDbCommand(mydb, connection)

            run.ExecuteNonQuery()

            'Display message box
            MsgBox("Appointment Added to Database.")

            'Close connection
            connection.Close()

            'Reset Form Fields
            AppointmentDatePicker.Text = ""
            AppointmentTimePicker.Text = ""
            AppointmentRealTimePicker.Text = ""
            chkOnTime.Checked = False
            chkSmallDelay.Checked = False
            chkLargeDelay.Checked = False
            chkEncouragement.Checked = False
            chkCourage.Checked = False
            chkSupport.Checked = False
            chkAcknowledgement.Checked = False
            chkSecurity.Checked = False
            chkUnderstanding.Checked = False
            chkCaring.Checked = False
            chkTrust.Checked = False
            chkConfidence.Checked = False
            chkAcceptance.Checked = False
            chkInvolvement.Checked = False
            chkSharing.Checked = False
            chkAngry.Checked = False
            txtComments.Text = ""
            AppointmentDatePicker.Focus()

        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Critical, "Oledb Error")

        End Try
    End Sub

    Private Sub mnuDisplayAppointmentSummary_Click(sender As System.Object, e As System.EventArgs) Handles mnuDisplayAppointmentSummary.Click
        'Part 1: Check if Table is Empty.

        Dim connection As OleDb.OleDbConnection
        Dim mydb, mystr As String

        mystr = ("Provider=Microsoft.JET.OLEDB.4.0;" & _
                 "Data Source=|DataDirectory|\Decisions.mdb")

        connection = New OleDb.OleDbConnection(mystr)

        connection.Open()

        mydb = "SELECT COUNT(*) FROM tblDecisions"

        Dim run = New OleDb.OleDbCommand

        run = New OleDbCommand(mydb, connection)


        Dim DataReader As OleDbDataReader

        DataReader = run.ExecuteReader() 'Populate the reader

        While (DataReader.Read())

            If DataReader(0).ToString() = 0 Then

                'Loop all the records

                MessageBox.Show("There are no Appointments added yet.")

                frmMain.Show()
                Me.Close()

            Else

                'Part 2: List items in table.

                'Load new form
                frmDisplayAppointments.Show()

                'Clears ListView before it is populated
                frmDisplayAppointments.ListViewDisplayAppointments.Items.Clear()

                'Populates ListView from Access Database
                Try
                    PopulateListViewFromAccess(frmDisplayAppointments.ListViewDisplayAppointments)
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try

                'Close current form
                Me.Close()

            End If

        End While

        '------------------------------------

        'Close connection
        'connection.Close()

    End Sub

    Private Sub mnuExit_Click(sender As System.Object, e As System.EventArgs) Handles mnuExit.Click
        'Exits program
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        'Reset Form Fields
        AppointmentDatePicker.Text = ""
        AppointmentTimePicker.Text = ""
        AppointmentRealTimePicker.Text = ""
        chkOnTime.Checked = False
        chkSmallDelay.Checked = False
        chkLargeDelay.Checked = False
        chkEncouragement.Checked = False
        chkCourage.Checked = False
        chkSupport.Checked = False
        chkAcknowledgement.Checked = False
        chkSecurity.Checked = False
        chkUnderstanding.Checked = False
        chkCaring.Checked = False
        chkTrust.Checked = False
        chkConfidence.Checked = False
        chkAcceptance.Checked = False
        chkInvolvement.Checked = False
        chkSharing.Checked = False
        chkAngry.Checked = False
        txtComments.Text = ""
        AppointmentDatePicker.Focus()
    End Sub

    Private Sub mnuStartPage_Click(sender As System.Object, e As System.EventArgs) Handles mnuStartPage.Click
        frmMain.Show()
        Me.Close()
    End Sub

    Private Sub mnuAbout_Click(sender As System.Object, e As System.EventArgs) Handles mnuAbout.Click
        frmAbout.Show()
    End Sub
End Class